package com.css.fxfzdzzh.modules.group.service.impl;

import com.css.fxfzdzzh.modules.group.entity.GroupEntity;
import com.css.fxfzdzzh.modules.group.entity.GroupUserEntity;
import com.css.fxfzdzzh.modules.group.entity.UserEntity;
import com.css.fxfzdzzh.modules.group.repository.GroupRepository;
import com.css.fxfzdzzh.modules.group.service.GroupService;
import com.css.fxfzdzzh.modules.group.service.GroupUserService;
import com.css.fxfzdzzh.modules.group.vo.GroupMemberVo;
import com.css.fxfzdzzh.modules.group.vo.GroupVo;
import com.css.fxfzdzzh.util.CreateGson;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.util.UUIDGenerator;
import com.css.fxfzdzzh.web.PlatformSessionContext;
import com.css.fxfzdzzh.zcpt.sys.service.impl.SUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author
 * @program
 * @description
 * @create 2022/7/26 10:30
 **/
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    GroupRepository groupRepository;

    @Autowired
    GroupUserService groupUserService;

    @Autowired
    SUserServiceImpl sUserServiceImpl;

    @Override
    @Transactional
    public void saveGroup(GroupMemberVo groupMemberVo) {
        GroupEntity fwGroupEntity = groupMemberVo.getFwGroupEntity();
        String groupId= UUIDGenerator.getUUID();
        fwGroupEntity.setId(groupId);
        fwGroupEntity.setDelFlag("0");

        Date date =new Date();
        fwGroupEntity.setCreateTime(date);
        //获取当前登陆人id
        fwGroupEntity.setCreateUser(PlatformSessionContext.getUserID());
        List<UserEntity> memberList = groupMemberVo.getMemberlist();
        StringBuilder memberId=new StringBuilder();
        StringBuilder memberName = new StringBuilder();
        memberList.forEach(userEntity -> {
            memberId.append(userEntity.getUuid()+",");
            memberName.append(userEntity.getRealName()+",");
        });
        fwGroupEntity.setGroupMember(memberName.toString().substring(0,memberName.toString().length()-1));
        String[] memberIdSplit = memberId.toString().substring(0, memberId.toString().length() - 1).split(",");
        List<String> memberIdList = Arrays.asList(memberIdSplit);

        List<GroupUserEntity> GroupUserEntitylist = new ArrayList<>();

        for(String userId:memberIdList){
            GroupUserEntity groupUserEntity=new GroupUserEntity();
            groupUserEntity.setCreateTime(new Date());
            groupUserEntity.setCreateUser(PlatformSessionContext.getUserID());
            groupUserEntity.setGroupId(groupId);
            groupUserEntity.setId(UUIDGenerator.getUUID());
            //身份标识，0普通成员，1专家
            groupUserEntity.setRole("0");
            groupUserEntity.setUserId(userId);
            GroupUserEntitylist.add(groupUserEntity);
        }
        List<UserEntity> expertList = groupMemberVo.getExpertlist();
        if(!PlatformObjectUtils.isEmpty(expertList) && expertList.size()>0) {
            StringBuilder expertId=new StringBuilder();
            StringBuilder expertName = new StringBuilder();
            expertList.forEach(userEntity -> {
                expertId.append(userEntity.getUuid()+",");
                expertName.append(userEntity.getRealName()+",");
            });

            fwGroupEntity.setGroupExpert(expertName.toString().substring(0,expertName.toString().length()-1));
            String[] expertIdSplit = expertId.toString().substring(0, expertId.toString().length() - 1).split(",");
            List<String> expertIdList = Arrays.asList(expertIdSplit);

            for(String userId:expertIdList){
                GroupUserEntity groupUserEntity=new GroupUserEntity();
                groupUserEntity.setCreateTime(new Date());
                groupUserEntity.setCreateUser(PlatformSessionContext.getUserID());
                groupUserEntity.setGroupId(groupId);
                groupUserEntity.setId(UUIDGenerator.getUUID());
                //身份标识，0普通成员，1专家
                groupUserEntity.setRole("1");
                groupUserEntity.setUserId(userId);
                GroupUserEntitylist.add(groupUserEntity);
            }
        }
        groupRepository.saveGroup(fwGroupEntity);
        groupUserService.saveGroupUser(GroupUserEntitylist);

    }

    @Override
    public Map<String,Object> getPage(HttpServletRequest request, GroupVo groupVo, int curPage, int pageSize) {
        Map<String,Object> page = groupRepository.getPage(request, groupVo, curPage, pageSize);
        return page;
    }

    @Override
    @Transactional
    public void deletGroup(List<String> ids) {
        Date updateTime = new Date();
        for(String id:ids){
            groupRepository.deletGroup(id, updateTime);
            groupUserService.deleteGroupIdAndUserId(id);
        }
    }



    @Override
    @Transactional
    public GroupMemberVo findById(String groupId) {
        GroupEntity groupEntity=groupRepository.selectGroup(groupId);
        GroupMemberVo groupMemberVo=new GroupMemberVo();
        groupMemberVo.setFwGroupEntity(groupEntity);
        List<UserEntity> memberList=new ArrayList<>();
        List<UserEntity> expertList=new ArrayList<>();

        List<GroupUserEntity> groupUserEntityList=groupUserService.selectGroupMemberAndGroupExpert(groupId);

        for(GroupUserEntity groupUserEntity:groupUserEntityList){
            String groupUserInfo = sUserServiceImpl.getGroupUserInfo(groupUserEntity.getUserId());
            UserEntity userInfo = CreateGson.createGson().fromJson(groupUserInfo, UserEntity.class);
            if("0".equals(groupUserEntity.getRole())){
                memberList.add(userInfo);
            }else{
                expertList.add(userInfo);
            }
        }
        groupMemberVo.setMemberlist(memberList);
        groupMemberVo.setExpertlist(expertList);
        return groupMemberVo;
    }

    @Override
    @Transactional
    public void updateGroup(GroupMemberVo groupMemberVo) {
        GroupEntity fwGroupEntity = groupMemberVo.getFwGroupEntity();
        String groupId = fwGroupEntity.getId();
        List<GroupUserEntity> GroupUserEntitylist = new ArrayList<>();
        fwGroupEntity.setUpdateUser(PlatformSessionContext.getUserID());
        fwGroupEntity.setUpdateTime(new Date());

        List<UserEntity> memberList = groupMemberVo.getMemberlist();
        StringBuilder memberId=new StringBuilder();
        StringBuilder memberName = new StringBuilder();
        memberList.forEach(userEntity -> {
            memberId.append(userEntity.getUuid()+",");
            memberName.append(userEntity.getRealName()+",");
        });
        fwGroupEntity.setGroupMember(memberName.toString().substring(0,memberName.toString().length()-1));
        List<UserEntity> expertList = groupMemberVo.getExpertlist();
        if(!PlatformObjectUtils.isEmpty(expertList) && expertList.size()>0){
            StringBuilder expertId=new StringBuilder();
            StringBuilder expertName = new StringBuilder();
            expertList.forEach(userEntity -> {
                expertId.append(userEntity.getUuid()+",");
                expertName.append(userEntity.getRealName()+",");
            });
            fwGroupEntity.setGroupExpert(expertName.toString().substring(0,expertName.toString().length()-1));
            String[] expertIdSplit = expertId.toString().substring(0, expertId.toString().length() - 1).split(",");
            List<String> expertIdList = Arrays.asList(expertIdSplit);
            for(String userId:expertIdList){
                GroupUserEntity groupUserEntity=new GroupUserEntity();
                groupUserEntity.setCreateTime(new Date());
                groupUserEntity.setCreateUser(PlatformSessionContext.getUserID());
                groupUserEntity.setGroupId(groupId);
                groupUserEntity.setId(UUIDGenerator.getUUID());
                //身份标识，0普通成员，1专家
                groupUserEntity.setRole("1");
                groupUserEntity.setUserId(userId);
                GroupUserEntitylist.add(groupUserEntity);
            }

        }

        groupRepository.updateGroup(fwGroupEntity);
        groupUserService.deleteGroupIdAndUserId(groupId);
        String[] memberIdSplit = memberId.toString().substring(0, memberId.toString().length() - 1).split(",");
        List<String> memberIdList = Arrays.asList(memberIdSplit);

        for(String userId:memberIdList){
            GroupUserEntity groupUserEntity=new GroupUserEntity();
            groupUserEntity.setCreateTime(new Date());
            groupUserEntity.setCreateUser(PlatformSessionContext.getUserID());
            groupUserEntity.setGroupId(groupId);
            groupUserEntity.setId(UUIDGenerator.getUUID());
            //身份标识，0普通成员，1专家
            groupUserEntity.setRole("0");
            groupUserEntity.setUserId(userId);
            GroupUserEntitylist.add(groupUserEntity);
        }

        groupUserService.saveGroupUser(GroupUserEntitylist);
    }



    @Override
    public List<GroupEntity> getGroupInfo(String groupLeaderId) {
        List<GroupEntity> groupInfo=groupRepository.getGroupInfo(groupLeaderId);
        return groupInfo;
    }
}
